home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12938 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: altair.dur.ac.uk!d40v94
  2. Newsgroups: comp.lang.c
  3. Subject: Help: array of ptrs to structures
  4. Message-ID: <Pine.SOL.3.91-941213.960403155531.22205C-100000@altair.dur.ac.uk>
  5. From: Ed Wiles <E.D.Wiles@durham.ac.uk>
  6. Date: Wed, 3 Apr 1996 16:18:31 +0100
  7. Organization: University of Durham, Durham, UK.
  8. NNTP-Posting-Host: altair.dur.ac.uk
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11.  
  12. [I'm a bit of a C novice, would appreciate your (combined) help, thanks!]
  13.  
  14. "struct person" contains three variables: lastname, firstname, phone
  15. (all arrays of char).
  16.  
  17. Within main(), I've declared an array of pointers to "struct person":
  18.    struct person *phone_book[6];
  19.  
  20. 1) How do I hard-code, for example, the third person in the array to be:
  21.    lastname = "Adcock";  firstname = "Lucy";  phone = "(01623) 943086"; ?
  22.  
  23.    *phone_book[2] = {"Adcock", "Lucy", etc.} is not allowed, so do I have to
  24.    assign each structure member individually?
  25.    i.e. *phone_book[2]->lastname = "Adcock", etc.
  26.  
  27.    (In fact, I really want to hard-code all the people in the array in one
  28.     go: is there a way of doing this when I declare the variable phone_book,
  29.     remembering that e.g. int odds[] = {1, 3, 5, 7} is allowed?)
  30.  
  31.    Also, the pointer phone_book[2] is uninitialised, so it could be the
  32.    address of anywhere... How do I initialise a pointer, please? (It is
  33.    NEW(pointer_name) in Modula-2, so there must be a C equivalent..?)
  34.  
  35. 2) I want to pass the entire array to a function. Do I just pass it as
  36.    phone_book? Should the function's corresponding formal parameter be
  37.    struct person *anyname[] ?
  38.  
  39. Thanks a lot!
  40.  
  41. - Ed Wiles (e.d.wiles@dur.ac.uk)
  42.